package com.shengyin.ec.controller.common;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.controller.account.PersonalForm;
import com.shengyin.ec.model.entity.ClusterCompany;
import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.service.account.BookInfoService;
import com.shengyin.ec.service.account.FavoritenService;
import com.shengyin.ec.service.account.PersonalService;
import com.shengyin.ec.service.account.ProvinceService;
import com.shengyin.ec.service.account.ReceiverService;
import com.shengyin.ec.service.alliance.AllianceService;
import com.shengyin.ec.service.alliance.AllianceViewInfoService;
import com.shengyin.ec.service.cluster.ClusterService;
import com.shengyin.ec.service.companyorder.CompanyOrderService;
import com.shengyin.ec.service.goods.GoodsService;
import com.shengyin.ec.service.order.OrderService;
import com.shengyin.ec.service.payment.PaymentService;
@Controller
public class LoginController {
	
	@Autowired
	PersonalService personalService;
	
	@Autowired
	ProvinceService provinceService;
	
	@Autowired
	ReceiverService receiverService;
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	FavoritenService favoritenService;
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	PaymentService PaymentService;
	
	@Autowired
	Md5PasswordEncoder passwordEncoder;

	@Autowired
	AllianceService allianceService;
	
	@Autowired
	ClusterService clusterService;
	
	
	@Autowired 
	CompanyOrderService companyOrderService;
	
	@Autowired 
	BookInfoService bookInfoService;
	
	//add by lcl 2012-10-10
	@Autowired
	AllianceViewInfoService allianceViewInfoService;
	
	
	//modify by lcl 2012-9-9 different type customer has different entry
			@RequestMapping("/checkUser.do")
			public String checkUser(Model model, PersonalForm personalForm, HttpServletRequest request,@RequestParam("type") int type){
				
				Personal personal1 = new Personal();
				String result = "redirect:";
				if(personalForm.getDirectUrl()!=null&&personalForm.getDirectUrl().startsWith("http://localhost")){
					result += personalForm.getDirectUrl();
				}else{
					result += "http://localhost:8080/ec/";
				}

				try{
					BeanUtils.copyProperties(personal1, personalForm);
					String password=passwordEncoder.encodePassword("shengyin", personal1.getPassword());
					//check the user is a common member or not
					boolean flag1 = personalService.checkUser(personal1.getUsername(), password);
					//check the user is a alliance member or not
					boolean flag2 = allianceService.checkCompanyUser(personal1.getUsername(), password);
					//we shold also at check league member here
					boolean flag3 = clusterService.checkCompanyUser(personal1.getUsername(), password);
					//we shold also at check alliance viewer member here
					boolean flag4 = allianceViewInfoService.checkAllianceViewer(personal1.getUsername(), password);
					
					if((type == 0)&&flag1){
						HttpSession session = request.getSession();
						Personal personal2 = personalService.getPersonalByUsername(personal1.getUsername()).get(0);
						
						session.setAttribute("user", personal2);
						session.setAttribute("typeLogin", 0);
						
						//return "/index";
						return result;
					}
					if((type == 1)&&flag2){
						
							HttpSession session = request.getSession();
						Company allianceList = allianceService.getCompanyByName(personal1.getUsername()).get(0);
						
						session.setAttribute("alliance", allianceList);
						session.setAttribute("typeLogin", 1);
						
						//return "/index";
						return result;
						
					}
					
					if((type == 2)&&flag3){
						
						ClusterCompany clusterCompany=clusterService.getClusterCompanyByUsername(personalForm.getUsername());
						
						System.out.println("clusterCompany==========="+clusterCompany.getId());
						if(clusterCompany.getStatus() == 2){
							
							HttpSession session = request.getSession();
							session.setAttribute("cluster", clusterCompany);
							session.setAttribute("typeLogin", 2);
							return "redirect:/view/cluster/clusterAccount.jsp";
						}
						if(clusterCompany.getStatus()==0)
						{  model.addAttribute("loger3","0");
						   System.out.println("cluster==========status");
						}   
						else
							model.addAttribute("loger3","1");
						
						model.addAttribute("tab","2");
						return result;
						
					}
					
					if((type == 3)&&flag4){
						int viewerType = allianceViewInfoService.getAllianceViewType(personal1.getUsername());
						
						//联盟信息浏览者
						if(viewerType == 0)
							return "redirect:/view/alliance/allianceViewerAccount.jsp";
						//联盟信息浏览者
						if(viewerType == 1)
							return "redirect:/view/alliance/alliancePosterAccount.jsp";
					}
					
					
					
						model.addAttribute("loger", "error");
						return "/view/login";
					
				}catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				

				return "/view/login";
			}
}
